﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Passwords.Biz;
using Silvermoon.Windows.Styles;
using Passwords.Utils;
using Silvermoon.Images;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Passwords.Images;
using Silvermoon.OpenGL;
using Passwords.Controls;

namespace Passwords.Pages
{
    /// <summary>
    /// Displays all available categories to choose one to edit.
    /// </summary>
    public class CategoryPickerPage : Page
    {
        private ListView listView;
        private ListViewController<IndexItem, Category> controller;
        private ContextMenu itemContextMenu;

        public CategoryPickerPage()
            : base()
        {
            InitializePage();
        }

        protected override void Dispose(bool dispose)
        {
            if (Context.Current != null)
            {
                Context.Current.CategoryDeleted -= OnCategoryDeleted;
            }
            base.Dispose(dispose);
        }

        private void InitializePage()
        {
            Title = "EDIT CATEGORIES";
            Label label = new Label { Style = StylePool.TitleLabel, Text = "categories", Margin = new Margin(20, 0, 20, null), Height = 80 - 8, Alignment = Alignment.Near, LineAlignment = Alignment.Far };
            listView = new ListView { Margin = new Margin(20, 80, 10, 4) };
            Styles = StylePool.Metro;
            Style = StylePool.EditWindow;
            Controls.Add(listView, label);
            controller = new ListViewController<IndexItem, Category>(listView) { ItemHeight = 100 };
            controller.ItemStyle = new TextItemStyle { Font = FontCache.MediumRegularFont, Padding = new Margin(0, 0, 24, 0) };
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<IndexItem>>(controller_UpdateItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<IndexItem>>(controller_InitializeItem);

            itemContextMenu = new ContextMenu();
            itemContextMenu.AddItem("Delete").Tap += new EventHandler<TapEventArgs>(CategoryPickerPage_Tap);

            Toolbar toolbar = new Toolbar();
            toolbar.AddButton(SysImages.Back, Page.BackCommand);
            toolbar.AddButton(SysImages.Add).Tap += new EventHandler<TapEventArgs>(OnAddNewCategory);
            this.Toolbar = toolbar;
            controller.DataSource = Context.Current.Categories;
            Context.Current.CategoryDeleted += new EventHandler<CategoryEventArgs>(OnCategoryDeleted);
        }

        void OnCategoryDeleted(object sender, CategoryEventArgs e)
        {
            controller.RemoveValue(e.Category);
        }

        void CategoryPickerPage_Tap(object sender, TapEventArgs e)
        {
            Item item = itemContextMenu.AnchorControl as Item;
            if (item != null)
            {
                Category category = item.DataValue as Category;
                category.DeleteCategory(null);
            }
        }

        void OnAddNewCategory(object sender, TapEventArgs e)
        {
            Category c = new Category();
            NewCategoryPage page = new NewCategoryPage { Category = c };
            this.NavigateTo(page);
        }

        void controller_InitializeItem(object sender, ItemControlGeneratorEventArgs<IndexItem> e)
        {
            IndexItem item = e.Item;
            item.Tap += new EventHandler<TapEventArgs>(Item_Tap);
            item.ContextMenu = itemContextMenu;
        }

        void Item_Tap(object sender, TapEventArgs e)
        {
            Item item = sender as Item;
            Category category = item.DataValue as Category;
            EditCategoryPage page = new EditCategoryPage { Category = category };
            this.NavigateTo(page);
        }

        void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<IndexItem> e)
        {
            IndexItem item = e.Item;
            Category c = e.Value as Category;
            item.Name = c.Name;
            item.Index = c.Passwords.Count;
        }

        private class Bag
        {
            public int YOffset;
        }

        public override object OnSaveViewState()
        {
            return new Bag { YOffset = listView.YOffset };
        }

        public override void OnLoadViewState(object viewState)
        {
            Bag bag = viewState as Bag;
            if (bag != null)
            {
                listView.YOffset = bag.YOffset;
            }
        }



    }
}
